<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="specName">
              <el-input v-model="filters.specName" placeholder="规格名称" clearable></el-input>
            </el-form-item>
            <el-form-item prop="specType">
              <el-select v-model="filters.specType" placeholder="规格类型" clearable>
                <el-option v-for="item in specTypeList" :value="item.id" :label="item.name" :key="item.id"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table ref="table" :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="specName" align="center" label="规格名称"></el-table-column>
        <el-table-column align="center" label="规格类型">
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" effect="dark" :type="formatSpecTypes(scope.row.specType)">
                {{ formatSpecType(scope.row.specType) }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="specUnit" align="center" label="规格单位"></el-table-column>
        <el-table-column prop="specDesc" align="center" label="规格介绍"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import SpecApi from '@/api/goods/spec';

export default {
  name: 'SpecManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        specName: '',
        specType: '',
      },
      specTypeList: [
        { id: 1, name: '产品' },
        { id: 2, name: '公共' },
      ],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_GOODS_SPEC_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_GOODS_SPEC_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_GOODS_SPEC_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_GOODS_SPEC_LIST_DELETE',
      permissionUpStatus: 'SYSTEM_MANAGER_GOODS_SPEC_LIST_STATE',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  components: {},
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },


    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        specName: _this.filters.specName,
        specType: _this.filters.specType,
      };
      // 发送请求
      SpecApi.listSpec(cond)
        .then((res) => {
          _this.listLoading = false;
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
          _this.$refs.table.bodyWrapper.scrollTop = 0;
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
    formatSpecType(cellValue) {
      if (cellValue === 1) {
        return '产品';
      } else if (cellValue === 2) {
        return '公共';
      }
    },
    formatSpecTypes(cellValue) {
      if (cellValue === 1) {
        return 'primary';
      } else if (cellValue === 2) {
        return 'success';
      }
    },
    formatState(cellValue) {
      if (cellValue === -1) {
        return '删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue === -1) {
        return 'danger';
      } else if (cellValue === 0) {
        return 'info';
      } else if (cellValue === 1) {
        return 'success';
      }
    },

    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/goods/specDetail',
        query: { id: row.specId },
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/goods/specEdit',
        query: { id: val.specId },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/goods/specAdd');
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
<style lang="scss" scoped>
.img-wrapper {
  width: 80px;
  height: 60px;
  overflow: hidden;
  border-radius: 3px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin: 0 auto;
  .el-image {
    width: 100%;
    &::v-deep img {
      width: 100%;
      height: auto;
    }
  }
}
</style>
